import copy
from rest_framework.views import APIView
from Notes.models import Note
from SecureHelperServer.settings import formatResponseData
from utils.conf import Response, checkUserLoginStatus
from utils.custom import MyJWTAuthentication


class GetNoteList(APIView):
    # 局部(本视图有效)认证类
    authentication_classes = [MyJWTAuthentication]
    # 局部(本视图有效)权限类
    permission_classes = []

    def post(self, request, *args, **kwargs):
        """
        获取NoteList
        """
        data = copy.deepcopy(formatResponseData)
        try:
            if checkUserLoginStatus(request=request):
                notes = Note.objects.filter(user=request.user)
            else:
                notes = []

            noteList = []
            for note in notes:
                noteObj = {
                    "id": note.id,
                    "content": note.content,
                    "timeStamp": note.timeStamp
                }
                noteList.append(noteObj)

            data["data"]["noteList"] = noteList
            return Response(data=data, loggerMsg=f"获取NoteList: 成功")

        except Exception as e:
            return Response(data=data, responseMsg="ServerError",
                            loggerMsg=f"请求处理失败: {e}", isError=True)
